﻿using System;
using System.Collections;
using App_Code.Modules;
using Portal.Core.Helper;
using Portal.Data.Entity;
using Telerik.WebControls;

public partial class Modules_Category_List : CategoryModule
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadTree();
        }

    }

    public void AjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
    {
        string[] args = e.Argument.Replace("\r\n", "\n").Split('\n');
        RadTreeView srcTree = null;
        RadTreeView dstTree = null;
        if (tree1.ClientID.Equals(args[0]))
        {
            srcTree = tree1;
        }

        if (tree1.ClientID.Equals(args[1]))
        {
            dstTree = tree1;
        }

        RadTreeNode sourceNode = srcTree.FindNodeByText(args[2]);
        RadTreeNode destNode = dstTree.FindNodeByText(args[3]);

        if (sourceNode.Parent == null)
        {
            RadAjaxManager1.Alert("Không thể di chuyển gốc!");
            return;
        }
        RadTreeNode tempNode = destNode;
        while (tempNode != null)
        {
            if (!tempNode.Equals(sourceNode))
                tempNode = tempNode.Parent;
            else
                break;
        }
        if (tempNode != null)
        {
            RadAjaxManager1.Alert("Không thể di chuyển cấp cha vào cấp con của nó!");
            return;
        }
        //RadTreeNode newNode = new RadTreeNode(sourceNode.Text, sourceNode.Value);
        UpdateCategory(sourceNode.Value, Convert.ToInt64(destNode.Value));   //Update moved category

        LoadTree();
    }
    private void UpdateCategory(string catEditID, Int64 newParentID)
    {
        Category catEdit = Category.FindByPrimaryKey(catEditID);
        catEdit.Parent_ID = newParentID;
        catEdit.Update();

        //clear cache from Category
        CacheHelper.Instance.RemoveByType(typeof(Category));
    }

    private void LoadTree()
    {
        tree1.Nodes.Clear();

        //first load Null Root
        RadTreeNode nullNode = new RadTreeNode("Root", "0");
        nullNode.Expanded = true;
        tree1.Nodes.Add(nullNode);

        AddChildNodes(nullNode);
    }
    private void AddChildNodes(RadTreeNode node)
    {
        var listChild = node.Value == "0" ? Category.FindRoots() : Category.FindSubCategories(Convert.ToInt64(node.Value));

        foreach (Category child in listChild)
        {
            RadTreeNode childnode = new RadTreeNode(child.FullName + " (" + child.Name + ")", child.ID.ToString());
            IList subs = Category.FindSubCategories(child.ID);
            if (subs.Count > 0)
            {
                //                childnode.Expanded = child.Level <= 0;
                childnode.Expanded = true;
                AddChildNodes(childnode);
            }
            node.Nodes.Add(childnode);
        }
    }

    protected void RadMenu1_ItemClick(object sender, RadMenuEventArgs e)
    {
        var selectCatid = Convert.ToInt64(tree1.SelectedNode.Value);
        Category slCat = Category.FindByPrimaryKey(selectCatid);
        switch (e.Item.Value)
        {
            case "Delete":
                Category delcat = Category.FindByPrimaryKey(selectCatid);
                delcat.Delete();
                tree1.SelectedNode.Remove();
                break;

            case "Add":
                Response.Redirect("admin_Category_category_list_new.aspx?catid=" + selectCatid, true);
                break;

            case "Edit":
                Response.Redirect("admin_Category_category_list_edit.aspx?id=" + selectCatid, true);
                break;

            case "EditListPage":

                Response.Redirect("admin_WebpartPage_WebpartPage_editpage.aspx?id=" + slCat.ListPage_ID, true);
                break;

            case "EditDetailPage":

                Response.Redirect("admin_WebpartPage_WebpartPage_editpage.aspx?id=" + slCat.DetailPage_ID, true);
                break;
        }
    }


}

